import { useSelector } from "react-redux";
import { RootState } from "../../store/rootReducer";
import { Navigate } from "react-router-dom";
import AccessEnum from "../../constant/AccessEnum";
import { message } from "antd";

interface IProps {
  children: JSX.Element;
  access?: AccessEnum;
}

export default function AuthRoute(props: IProps) {
  const loginUser = useSelector((state: RootState) => state.loginUser);
  const { children, access = AccessEnum.USER } = props;
  if (loginUser.id == -1) {
    message.error('请先登录');
    return <Navigate to="/login" replace />;
  }

  if (access === AccessEnum.ADMIN && loginUser.userRole !== AccessEnum.ADMIN) {
    return <Navigate to="/404" replace />;
  }

  return children;
} 